import router from '@/router'
import {
  getToken
} from '@/utils/auth'
import { isMobileWeixin } from "./func"
import Vue from 'vue'
const $this = new Vue()

router.beforeEach(async (to, from, next) => {
    if (isMobileWeixin()) {
      window.location.href = "http://www.wx.huibaohulian.com";
    }
  if (localStorage.getItem("username") && localStorage.getItem("token")) {
    $this.$get("/customer/getInfo").then((res) => {
      localStorage.setItem('userInfo', JSON.stringify(res.data))
    });
  }

  // 获取token
  const hasToken = getToken()
  if (hasToken) {
    if (to.path === '/login') {
      // 已经登录，跳转到首页
      next({
        path: '/'
      })
    } else {
      next()
    }
  } else {
    /* has no token */
    if (to.meta.isOpen) {
      // 开放页面，无需验证
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/`)
    }
  }
})